﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace WPF数据采集系统
{
    /// <summary>
    /// CustomMsgBox.xaml 的交互逻辑
    /// </summary>
    public partial class CustomMsgBox : Window
    {
        public string CustomImageSource = "报警标志.png";
        public event EventHandler<MessageBoxEventArgs> Result;
        public CustomMsgBoxButton cmb { get; set; }
        public CustomMsgBox()
        {
            InitializeComponent();
        }
        public static MessageResult ShowDialog(string title, string msg, CustomMsgBoxButton btn, CustomMsgBoxImage image)
        {
            CustomMsgBox mb = new CustomMsgBox();
            mb.customMsgBoxTitle.Text = title;
            mb.msg.Text = msg;
            switch (image)
            {
                case CustomMsgBoxImage.Error:
                    mb.CustomImageSource = "Error.png";
                    break;
                case CustomMsgBoxImage.Success:
                    mb.CustomImageSource = "successful.png";
                    break;
            }
            mb.imageControl.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\image\\" + mb.CustomImageSource));
            if (btn == CustomMsgBoxButton.One)
            {
                mb.btn_cancel.Visibility = Visibility.Hidden;
            }
            MessageResult r = null;
            mb.Result += (s, e) =>
            {
                r = e.Result;
            };
            mb.ShowDialog();
            return r;
        }
        public static MessageResult ShowDialog(string msg)
        {
            CustomMsgBox mb = new CustomMsgBox();
            mb.customMsgBoxTitle.Text = "警告";
            mb.msg.Text = msg;
            mb.imageControl.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\image\\" + mb.CustomImageSource));
            mb.btn_cancel.Visibility = Visibility.Hidden;
            MessageResult r = null;
            mb.Result += (s, e) =>
            {
                r = e.Result;
            };
            mb.ShowDialog();
            return r;
        }
        public static MessageResult ShowDialog(string title, string msg)
        {
            CustomMsgBox mb = new CustomMsgBox();
            mb.customMsgBoxTitle.Text = title;
            mb.msg.Text = msg;
            mb.imageControl.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\image\\" + mb.CustomImageSource));
            mb.btn_cancel.Visibility = Visibility.Hidden;
            MessageResult r = null;
            mb.Result += (s, e) =>
            {
                r = e.Result;
            };
            mb.ShowDialog();
            return r;
        }
        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
            Result?.Invoke(this, new MessageBoxEventArgs() { Result = new MessageResult() { IsYes = true } });
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Result?.Invoke(this, new MessageBoxEventArgs() { Result = new MessageResult() { IsYes = false } });
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
    }
    public enum CustomMsgBoxButton
    {
        One = 1, Two = 2, Three = 3
    }
    public enum CustomMsgBoxImage
    {
        Alert = 0, Error = 1, Success = 2
    }
    public class MessageBoxEventArgs : EventArgs
    {
        /// <summary>
        /// 结果，Yes为true，No为false
        /// </summary>
        public MessageResult Result { get; set; }
    }
    public class MessageResult
    {
        /// <summary>
        /// 结果，Yes为true，No为false
        /// </summary>
        public bool IsYes { get; set; }
    }
}
